<?php
/**
 * Created by PhpStorm.
 * User: dolia
 * Date: 15/12/26
 * Time: 下午3:34
 */

namespace App\Repositories;
use App\Interfaces\AddressService;
use App\Address;
//use InvalidArgumentException;
use App\Exceptions\Interfaces\AddressServiceException;

use Log;
use Auth;



class AddressRepository implements  AddressService
{

    /**
     * 当前登录用户id
     * @var
     */
    protected  $user_id;


    public function __construct(){

        $user = Auth::user();


        if($user){
            $user_id = $user->id;

        }else{

            throw new AddressServiceException("收货人参数user_id不能为空，请登录");
        }//end if()

        $this->init($user_id);

    }

    public function init($user_id){

        if (empty($user_id))
        {
            throw new CartServiceException("user_id不存在");
        }

        $this->user_id = $user_id;

    }

    /**
     * 获取用户收货地址
     * @param string $userId
     * @return mixed  包含: 1.用户所有的收货地址
     */
    public function getAddressInfo($user_id){

        $address = Address::where('user_id', $user_id)->orderBy('is_default', 'desc')->paginate(20);

        return $address;
    }//end  func getAddress


    public function getUserAddress($user_id){


        $address = Address::where('user_id', $user_id)->orderBy('is_default', 'desc')->get();

        return $address;

    }

    /**
     * 根据收货地址id获取用户收货地址
     * @param int $address_id      收货地址id
     * @return mixed  包含: 1.用户的单个收货地址
     */
    public function getAddress($address_id){

        $address = Address::where('user_id',Auth::id())->where('address_id',$address_id)->first();

        if(!$address){
            throw  new AddressServiceException('收货地址不存在');
        }
        return $address;

    }//end  func getAddress


    /**
     * 增加收货地址
     * @param int $user_id      用户id
     * @param array $addressArr   收货地址信息数组
     * @return mixed
     */
    public function addAddress($user_id, Array $addressArr){
        $addressCount = Address::where('user_id', $user_id)->count();
        if($addressCount>19){

            throw new AddressServiceException("收货地址最多只能添加20个");
        }//end if()

        foreach ($addressArr as $key=>$item) {

            $$key = $item;
        }//end foreach

        if(empty($user_id)){

            throw new AddressServiceException("收货人参数不能为空");
        }//end if()



        if(empty($consignee)){

            throw new AddressServiceException("收货人参数不能为空");
        }//end if()

        /*if(empty($country)){

            throw new AddressServiceException("收货地址国家参数country不能为空");
        }//end if()

        if(empty($province)){

            throw new AddressServiceException("收货地址省份参数province不能为空");
        }//end if()*/

        if(empty($province)){

            throw new AddressServiceException("收货地址省份参数不能为空");
        }//end if()

        if(empty($city)){

            throw new AddressServiceException("收货地址城市参数不能为空");
        }//end if()


        if(empty($district)){

            throw new AddressServiceException("收货地址区域参数不能为空");
        }//end if()

        if(empty($address)){

            throw new AddressServiceException("收货地址街道参数不能为空");
        }//end if()

        if(empty($mobile)){

            throw new AddressServiceException("收货人手机号码参数不能为空");
        }//end if()

        if(!empty($is_default) && $is_default==1){

            Address::where('user_id', $user_id)->update(['is_default'=>0]);

        }else{

            $is_default = 0;
        }//end if()

        $addressArr['user_id'] = $user_id;

        $address_id = Address::insertGetId($addressArr);

        $address = Address::where('user_id', $user_id)->where('is_default', 1)->get();
        if($address->count()==0){
            Address::find($address_id)->update(['is_default'=>1]);
        }//end if


        return $address_id;
    }//end  func addAddress


    /**
     * 删除收货地址
     * @param int $address_id     收货地址id
     * @param array $addressArr   要删除的收货地址信息数组
     * @return mixed
     */
    public function removeAddress($address_id){

        if(empty($address_id)){

            throw new AddressServiceException("要删除的收货地址id不能为空");

        }//end if()

        $addressObj = Address::find($address_id)->delete();

        //return $addressObj;

    }//end  func removeAddress


    /**
     * 修改收货地址
     * @param int $address_id     收货地址id
     * @param array $addressArr   要修改的收货地址信息数组
     * @return mixed
     */
    public function updateAddress($address_id, Array $addressArr){

        $user_id = $this->user_id;
        if(empty($address_id)){

            throw new AddressServiceException("要修改的收货地址id不能为空");

        }//end if()

        if(empty($addressArr)){

            throw new AddressServiceException("要修改的收货地址字段数组不能为空");

        }//end if()

        foreach ($addressArr as $key=>$item) {

            $$key = $item;

            if (empty($item) && $key!='is_default') {

                throw new AddressServiceException("参数值不能为空");
            }
        }//end foreach

        if ((isset($province) || isset($city) || isset($district)) && empty($position_ids)) {

            throw new AddressServiceException("地址组合参数不能为空");
        }

        /*if(empty($user_id)){

            throw new AddressServiceException("收货人参数不能为空");
        }//end if()*/



        if(!empty($is_default) && $is_default==1){

            Address::where('user_id', $user_id)->update(['is_default'=>0]);

        }else{

            $is_default = 0;
        }//end if()

        $addressObj = Address::where('user_id', $user_id)->where('address_id', $address_id)->first();

        if (empty($addressObj)) {

            throw new AddressServiceException("收货地址与该用户不匹配");
        }

        if(!empty($addressArr['is_default']) && $addressArr['is_default']==1){
            Address::where('user_id', $user_id)->where('address_id','!=',$address_id)->update(['is_default'=>0]);
        }//end if()

        unset($addressArr['_token']);
        $addressObj = $addressObj->where('address_id',$address_id)->update($addressArr);

        //return $addressObj;

    }//end  func updateAddress

    /**
     * 获取用户默认地址
     * @param int $user_id     用户id
     * @return mixed
     */
    public function getUserDefaultAddress($user_id){

        if(empty($user_id)){

            throw new AddressServiceException("用户的id不能为空");

        }//end if()

        $address = Address::where('user_id', $user_id)->where('is_default', 1)->first();

        //如果没有默认地址，按时间排序将最早的设为默认地址
        if(empty($address)){
            $address = Address::orderBy('address_id', 'asc')->where('user_id', $user_id)->first();
            if($address){
                $address_id = $address->address_id;
                Address::where('address_id', $address_id)->update(['is_default'=>1]);

                $address = Address::where('user_id', $user_id)->where('is_default', 1)->first();
            }

        }//end func

        return $address;
    }//end func getUserDefaultAddress

}//end class